using SplineMesh;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelMgr : MonoBehaviour
{
    public static LevelMgr Instance = null;
    [SerializeField] private Taxi Card;
    [SerializeField] private Transform TrfTarget;
    [SerializeField] private Spline SplineLoad;
    [SerializeField] private float MinSpeed = 1;
    [SerializeField] private float MaxSpeed = 10;
    [SerializeField] private float AcceUp = 16;
    [SerializeField] private float AcceDown = 16;
    [SerializeField] private float InitLength = 20;
    [HideInInspector] public GameState State = GameState.Idle;
    [HideInInspector] public int CustomerNumber = 0;
    private float _curSpeed = 1;
    private float _loadLength = 0;
    private float _curLength = 0;
    private Rigidbody _rigidbody;

    private void Awake()
    {
        Instance = this;
        _rigidbody = Card.GetComponent<Rigidbody>();
    }

    private void Start()
    {
        _loadLength = SplineLoad.Length;
        _curLength = InitLength;
        CurveSample sample = SplineLoad.GetSampleAtDistance(_curLength);
        Card.transform.position = sample.location;
        Card.transform.rotation = sample.Rotation;
        CustomerNumber = transform.GetComponentsInChildren<House>().Length / 2;
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (State == GameState.Idle)
            {
                StartGame();
            }
            else if (State == GameState.Stop)
            {
                StartGame();
            }
        }
    }

    private void StartGame()
    {
        State = GameState.Running;
        _curSpeed = MinSpeed;
    }

    private void FixedUpdate()
    {
        if (State == GameState.Running || State == GameState.Over)
        {
            if (Input.GetMouseButton(0) && State == GameState.Running)
            {
                if (_curSpeed < MaxSpeed)
                {
                    _curSpeed += AcceUp * Time.fixedDeltaTime;
                    if (_curSpeed > MaxSpeed)
                    {
                        _curSpeed = MaxSpeed;
                    }
                }
            }
            else
            {
                if (_curSpeed > MinSpeed)
                {
                    _curSpeed -= AcceDown * Time.fixedDeltaTime;
                    if (_curSpeed < MinSpeed)
                    {
                        _curSpeed = MinSpeed;
                    }
                }
            }
            if (State == GameState.Stop)
            {
                _curSpeed = 0;
            }
            else
            {
                _curLength += _curSpeed * Time.fixedDeltaTime;
                _curLength = Mathf.Clamp(_curLength, 0, _loadLength);
                CurveSample sample = SplineLoad.GetSampleAtDistance(_curLength);
                Card.transform.position = sample.location;
                Card.transform.rotation = sample.Rotation;
            }
        }
        if (State != GameState.Over && State != GameState.Die)
        {
            TrfTarget.position = Card.transform.position;
        }
    }
}
